var express = require('express');
var router = express.Router();
var _ = require('underscore');
var User = require('../models/user');

router.post("/signup", function (req, res, next) {
    var userObj = req.body.user;
    var _user;

    _user = new User(userObj);
    User.find({name:userObj.name}, function(err, user){
        if(user.length){
            res.redirect("/");
        }else{
            _user.save(function (err, user) {
                console.log("save success!")
            });
        }
    });
})
router.post("/signin", function (req, res, next) {
    var userObj = req.body.user;
    var _user;

    _user = new User(userObj);
    User.find({name:userObj.name}, function(err, user){
        if(user.length){
            req.session.user = user;
            res.redirect("/");
            console.log("login success!")
        }else{
            res.redirect("/");
        }
    });
})

module.exports = router;